package com.zgroup.controllers

import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;
import java.util.HashMap;
import java.util.Map;
import java.net.URLDecoder;

import com.zgroup.common.CheckUtils;
import com.zgroup.common.CipherUtils;
import com.zgroup.common.Constants;
import com.zgroup.common.WebUtils;
import com.zgroup.common.DicUtils;
import com.zgroup.common.PaginationAdapter;

import com.zgroup.services.DictionaryService;
import com.zgroup.domain.Dictionary;

class DictionaryController {
  def dictionaryService

  def showEstateList = {
    int pageNumber = 1;
    boolean onSelect = true;
    if (params.page != null) {
      pageNumber = Integer.parseInt(params.page);
    }
    String searchString = params.sourceValue;
    searchString = URLDecoder.decode(searchString, "utf-8");
    List list = null;
    if (CheckUtils.isEmpty(searchString)) {
//            Map map = new HashMap();
//            map.put("DM", "请输入关键字");
//            list = new ArrayList();
//            list.add(map);
//            onSelect=false;
      render ""
      return;
    } else {
      list = dictionaryService.loadEstate(params);
    }
    PaginationAdapter paginatedList = DicUtils.adapterDic(list, params.parent, searchString, pageNumber, params.filter);
    list = paginatedList.getList();
    if (list.isEmpty()) {
      Map map = new HashMap();
      if ("0" == params.ignoreBlank) {
        map.put("DM", "");
        map.put("MC", "没有找到相关信息,请添加");
        onSelect = true;
      } else {
        map.put("DM", "没有找到相关信息");
        onSelect = false;
      }

      list = new ArrayList();
      list.add(map);
      searchString = "";

      paginatedList.setList(list)
      paginatedList.setFullListSize(1);
      paginatedList.setPageNumber(1);
    }
    def dicHtml = DicUtils.showDictionay(request, response, paginatedList, onSelect);
    render(text: dicHtml, contentType: "text/html", encoding: "UTF-8")
  }

  def showDomainList = {
    int pageNumber = 1;
    boolean onSelect = true;
    if (params.page != null) {
      pageNumber = Integer.parseInt(params.page);
    }
    String searchString = params.sourceValue;
    searchString = URLDecoder.decode(searchString, "utf-8");
    List list = null;
    if (CheckUtils.isEmpty(searchString)) {
//            Map map = new HashMap();
//            map.put("DM", "请输入关键字");
//            list = new ArrayList();
//            list.add(map);
//            onSelect=false;
      render ""
      return;
    } else {
      list = dictionaryService.loadDomain(params.domain, params.sortKey);
    }
    PaginationAdapter paginatedList = DicUtils.adapterDic(list, params.parent, searchString, pageNumber, params.filter);
    list = paginatedList.getList();
    if (list.isEmpty()) {

      Map map = new HashMap();
      if ("0" == params.ignoreBlank) {
        map.put("DM", "");
        map.put("MC", "没有找到相关信息,请添加");
        onSelect = true;
      } else {
        map.put("DM", "没有找到相关信息");
        onSelect = false;
      }

      list = new ArrayList();
      list.add(map);
      searchString = "";
      paginatedList.setList(list)
      paginatedList.setFullListSize(1);
      paginatedList.setPageNumber(1);
    }
    def dicHtml = DicUtils.showDictionay(request, response, paginatedList, onSelect);

    render(text: dicHtml, contentType: "text/html", encoding: "UTF-8")
  }

  def showList = {
    int pageNumber = 1;
    boolean onSelect = true;
    if (params.page != null) {
      pageNumber = Integer.parseInt(params.page);
    }
    String searchString = params.sourceValue;
    searchString = URLDecoder.decode(searchString, "utf-8");
    List list = dictionaryService.loadDic(params.dicName);
    PaginationAdapter paginatedList = DicUtils.adapterDic(list, params.parent, searchString, pageNumber, params.filter);

    def dicHtml = DicUtils.showDictionay(request, response, paginatedList, onSelect);

    render(text: dicHtml, contentType: "text/html", encoding: "UTF-8")

  }

  def showAllEstateList = {
    int pageNumber = 1;
    boolean onSelect = true;
    if (params.page != null) {
      pageNumber = Integer.parseInt(params.page);
    }
    String searchString = params.sourceValue;
    searchString = URLDecoder.decode(searchString, "utf-8");
    List list = dictionaryService.loadEstate(params);
    PaginationAdapter paginatedList = DicUtils.adapterDic(list, params.parent, searchString, pageNumber, params.filter);

    def dicHtml = DicUtils.showDictionay(request, response, paginatedList, onSelect);

    render(text: dicHtml, contentType: "text/html", encoding: "UTF-8")

  }


}
